package com.easefun.polyv.livecloudclass.modules.chatroom.widget;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.easefun.polyv.livecloudclass.R;
import com.easefun.polyv.livecloudclass.modules.chatroom.chatmore.PLVLCChatFunctionListener;
import com.easefun.polyv.livecloudclass.modules.chatroom.chatmore.PLVLCChatMoreAdapter;
import com.lxj.xpopup.core.BottomPopupView;
import com.plv.livescenes.model.interact.PLVChatFunctionVO;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import static com.plv.foundationsdk.utils.PLVSugarUtil.arrayListOf;

/**
 * date: 2023-07-06
 * author: zhy
 * description: 更多
 */
public class PLVMoreDialogView extends BottomPopupView {
    // <editor-fold defaultstate="collapsed" desc="变量">
    //功能列表
    private RecyclerView chatMoreRv;
    //关闭按钮
    private ImageView plvIvPointRewardClose;
    //功能列表适配器
    private PLVLCChatMoreAdapter adapter;
    /**
     * 只看讲师
     */
    public static final String CHAT_FUNCTION_TYPE_ONLY_TEACHER = "CHAT_FUNCTION_TYPE_ONLY_TEACHER";
    /**
     * 公告
     */
    public static final String CHAT_FUNCTION_TYPE_BULLETIN = "CHAT_FUNCTION_TYPE_BULLETIN";
    /**
     * 消息
     */
    public static final String CHAT_FUNCTION_TYPE_MESSAGE = "CHAT_FUNCTION_TYPE_MESSAGE";
    /**
     * 屏蔽/展示 特效
     */
    public static final String CHAT_FUNCTION_TYPE_EFFECT = "CHAT_FUNCTION_TYPE_EFFECT";
    //每行显示的功能模块数量
    public static int LAYOUT_SPAN_COUNT = 4;
    //初始化支持的功能模块
    private final ArrayList<PLVChatFunctionVO> functionList = arrayListOf(
            new PLVChatFunctionVO(CHAT_FUNCTION_TYPE_BULLETIN, R.drawable.icon_only_gonggao, "公告", true),
            new PLVChatFunctionVO(CHAT_FUNCTION_TYPE_ONLY_TEACHER, R.drawable.icon_only_zhubo, "只看讲师", true),
            new PLVChatFunctionVO(CHAT_FUNCTION_TYPE_MESSAGE, R.drawable.icon_zhibo_jieshao, "直播介绍", false),
            new PLVChatFunctionVO(CHAT_FUNCTION_TYPE_MESSAGE, R.drawable.icon_zhibo_jieshao, "直播介绍", false)

    );
    //功能响应监听
    private PLVLCChatFunctionListener functionListener;

    private Context mContext;


    public PLVMoreDialogView(@NonNull @NotNull Context context,PLVLCChatFunctionListener functionListener) {
        super(context);
        this.mContext = context;
        this.functionListener = functionListener;
    }

    // </editor-fold >

    @Override
    protected int getImplLayoutId() {
        return R.layout.plv_point_more_window;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        chatMoreRv = findViewById(R.id.plvlc_chat_more_rv);
        plvIvPointRewardClose = findViewById(R.id.plv_iv_point_reward_close);
        if (functionList.size() >= 4) {
            LAYOUT_SPAN_COUNT = 4;
        } else {
            LAYOUT_SPAN_COUNT = functionList.size();
        }
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, LAYOUT_SPAN_COUNT);
        chatMoreRv.setLayoutManager(layoutManager);
        adapter = new PLVLCChatMoreAdapter(LAYOUT_SPAN_COUNT, mContext);
        adapter.setData(functionList);
        adapter.setListener(new PLVLCChatMoreAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String type) {
                dismiss();
                if (functionListener != null) {
                    String data = String.format("{\"event\" : \"%s\"}", type);
                    functionListener.onFunctionCallback(type, data);
                }
            }
        });
        chatMoreRv.setAdapter(adapter);
        plvIvPointRewardClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
    public PLVChatFunctionVO getFunctionByType(String type) {
        for (PLVChatFunctionVO functionVO : functionList) {
            if (type.equals(functionVO.getType())) {
                return functionVO;
            }
        }
        return null;
    }
    public void updateFunctionStatus(@NonNull PLVChatFunctionVO functionVO) {
        for (int i = 0; i < functionList.size(); i++) {
            if (functionVO.getType() != null && functionVO.getType().equals(functionList.get(i).getType())) {
                functionList.set(i, functionVO);
                break;
            }
        }
        adapter.updateFunctionList(functionList);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        onShowOrDismissListener.onDismiss();
    }

    @Override
    protected void onShow() {
        super.onShow();
        onShowOrDismissListener.onShow();
    }
    private OnShowOrDismissListener onShowOrDismissListener;

    public void setOnShowOrDismissListener(OnShowOrDismissListener onShowOrDismissListener) {
        this.onShowOrDismissListener = onShowOrDismissListener;
    }

    public interface OnShowOrDismissListener{
        void onShow();
        void onDismiss();
    }
}
